package com.clotingshoppos.modules.Shiro.controller;

import com.clotingshoppos.common.util.RetResponse;
import com.clotingshoppos.common.util.RetResult;
import com.clotingshoppos.modules.Shiro.repository.UserRepository;
import com.clotingshoppos.modules.Shiro.service.RoleService;
import com.clotingshoppos.modules.Shiro.util.JWTUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName: LoginController
 * @Description: 处理登陆的控制器
 * @Author: Izzat
 * @Date: 2019-04-14 22:04
 * @Version: v1.0
 */
@RestController
public class LoginController {
    @Autowired
    private UserRepository userRepository;


    @PostMapping("/login")
    public RetResult login(@RequestParam("username") String username,
                           @RequestParam("password") String password) {
        String realPassword = userRepository.getPassword(username);
        if (realPassword == null) {
            return new RetResponse().makeErrRsp("用户名出错").setCode(401);
        } else if (!realPassword.equals(password)) {
            return new RetResponse().makeErrRsp("密码出错").setCode(401);
        } else {
            return new RetResponse().makeOKRsp().setCode(200).setMsg(JWTUtil.createToken(username));
        }
    }

    @RequestMapping(path = "/unauthorized/{message}")
    public RetResult unauthorized(@PathVariable String message) throws UnsupportedEncodingException {
        System.out.println("maybe here?");
        return new RetResponse().makeOKRsp().setCode(401).setMsg(message);
    }
}